package com.hhq.demo.dao.impl;

import java.util.Date;

import com.hhq.demo.bean.Courier;
import com.hhq.demo.bean.CourierList;
import com.hhq.demo.dao.CourierDao;
import com.hhq.demo.utils.DateFormatUtil;
import com.hhq.demo.utils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HHQ
 * @date 2021年12月16日
 */
public class CourierDaoImpl implements CourierDao {

    private static final String UPDATE_USERNAME_BY_PHONE = "update courier set username=? where phone=?";
    private String CONSOLE_DATA = "select count(id) total_courier,count(TO_DAYS(register_time)=TO_DAYS(NOW()) or null) today_register_courier from courier";
    private String LIST_ALL = "select * from courier";
    private String LIST_LIMIT = "select * from courier limit ?,?";
    private String FIND_BY_PHONE = "select id,username,phone,id_number,password from courier where phone=?";
    /**
     * 新增快递员
     * 初始值为0                 注册时登录时间为null
     * username,phone,id_number,password,(express_number),register_time,(login_time)
     */
    private String INSERT = "insert into courier values(null,?,?,?,?,0,?,?)";
    private String UPDATE = "update courier set username=?,id_number=?,password=? where id=?";
    private String UPDATE_AND_PHONE = "update courier set username=?,phone=?,id_number=?,password=? where id=?";
    private String DELETE_BY_ID = "delete from courier where id=?";

    /**
     * 查询控制台需要的数据
     *
     * @return map集合数据
     */
    @Override
    public Map<String, Object> consoleData() {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        HashMap<String, Object> map = new HashMap<>();
        try {
            preparedStatement = connection.prepareStatement(CONSOLE_DATA);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                map.put("totalCourier", resultSet.getInt("total_courier"));
                map.put("todayRegisterCourier", resultSet.getInt("today_register_courier"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return map;
    }

    /**
     * 查询所有快递员信息
     *
     * @return 快递员集合
     */
    @Override
    public List<CourierList> listAll() {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<CourierList> courierList = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(LIST_ALL);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                CourierList courier = new CourierList();
                courier.setId(resultSet.getInt("id"));
                courier.setUsername(resultSet.getString("username"));
                courier.setPhone(resultSet.getString("phone"));
                courier.setIdNumber(resultSet.getString("id_number"));
                courier.setPassword(resultSet.getString("password"));
                courier.setExpressNumber(resultSet.getInt("express_number"));
                courier.setRegisterTime(DateFormatUtil.format(resultSet.getDate("register_time")));
                courier.setLoginTime(DateFormatUtil.format(resultSet.getDate("login_time")));
                courierList.add(courier);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return courierList;
    }

    /**
     * 分页查询快递员信息
     *
     * @param offset     起始索引
     * @param pageNumber 每页显示数量
     * @return 快递员信息集合
     */
    @Override
    public List<CourierList> listLimit(Integer offset, Integer pageNumber) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<CourierList> courierList = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(LIST_LIMIT);
            preparedStatement.setObject(1, offset);
            preparedStatement.setObject(2, pageNumber);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                CourierList courier = new CourierList();
                courier.setId(resultSet.getInt("id"));
                courier.setUsername(resultSet.getString("username"));
                courier.setPhone(resultSet.getString("phone"));
                courier.setIdNumber(resultSet.getString("id_number"));
                courier.setPassword(resultSet.getString("password"));
                courier.setExpressNumber(resultSet.getInt("express_number"));
                courier.setRegisterTime(DateFormatUtil.format(resultSet.getDate("register_time")));
                courier.setLoginTime(DateFormatUtil.format(resultSet.getDate("login_time")));
                courierList.add(courier);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return courierList;
    }

    /**
     * 新增快递员
     *
     * @param courier 快递员
     * @return 受影响的行数
     */
    @Override
    public int insert(Courier courier) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(INSERT);
            //username,phone,id_number,password,(express_number),register_time,(login_time)
            //insert into courier values(null,?,?,?,?,0,?,null)
            preparedStatement.setObject(1,courier.getUsername());
            preparedStatement.setObject(2,courier.getPhone());
            preparedStatement.setObject(3,courier.getIdNumber());
            preparedStatement.setObject(4,courier.getPassword());
            preparedStatement.setObject(5,new Date());
            preparedStatement.setObject(6,new Date());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
        return 0;
    }

    /**
     * 修改快递员信息
     *
     * @param id      要修改的快递员的id
     * @param courier 要修改的快递员的信息
     * @return 受影响的行数
     */
    @Override
    public int update(Integer id, Courier courier) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {

            //username=?,id_number=?,password=? where id=?
            preparedStatement = connection.prepareStatement(UPDATE);
            preparedStatement.setObject(1, courier.getUsername());
            preparedStatement.setObject(2, courier.getIdNumber());
            preparedStatement.setObject(3, courier.getPassword());
            preparedStatement.setObject(4, id);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
        return 0;
    }

    /**
     * 修改快递员信息包括手机号
     *
     * @param id      快递员id
     * @param courier 快递员信息
     * @return 受影响的行数
     */
    @Override
    public int updateAndPhone(Integer id, Courier courier) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            //"update courier set username=?,phone=?,id_number=?,password=? where id=?"
            preparedStatement = connection.prepareStatement(UPDATE_AND_PHONE);
            preparedStatement.setObject(1, courier.getUsername());
            preparedStatement.setObject(2, courier.getPhone());
            preparedStatement.setObject(3, courier.getIdNumber());
            preparedStatement.setObject(4, courier.getPassword());
            preparedStatement.setObject(5, id);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
        return 0;
    }

    /**
     * 根据id删除快递员
     *
     * @param id id
     * @return 受影响的行数
     */
    @Override
    public int deleteById(Integer id) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(DELETE_BY_ID);
            preparedStatement.setObject(1,id);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
        return 0;
    }

    /**
     * 根据手机号查询快递员信息
     *
     * @param phone 快递员手机号
     * @return 快递员信息
     */
    @Override
    public Courier findByPhone(String phone) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Courier courier = new Courier();
        try {
            preparedStatement = connection.prepareStatement(FIND_BY_PHONE);
            preparedStatement.setObject(1,phone);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                //id,username,phone,id_number,password
                courier.setId(resultSet.getInt("id"));
                courier.setUsername(resultSet.getString("username"));
                courier.setPhone(resultSet.getString("phone"));
                courier.setIdNumber(resultSet.getString("id_number"));
                courier.setPassword(resultSet.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return courier;
    }

    /**
     * 根据手机号码修改用户名
     *
     * @param phone    手机号码
     * @param username 要修改的用户信息
     */
    @Override
    public void updateUsername(String phone, String username) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(UPDATE_USERNAME_BY_PHONE);
            preparedStatement.setObject(1,username);
            preparedStatement.setObject(2,phone);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
    }
}
